package com.ivi.car.module.carstate.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ivi.car.R;
import com.ivi.car.base.BaseFragment;

import com.ivi.car.bean.carstate.CarPartState;
import com.ivi.car.module.carstate.model.IStateContract;
import com.ivi.car.module.carstate.presenter.StatePresenter;
import com.ivi.car.module.carstate.view.activity.PartStateActivity;
import com.wm.machine.baselibrary.common.widget.recyclerview.itemdecoration.SpaceItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Copyright
 * author chengxp
 * date 2018/3/7
 * describe 展示车辆部件状况
 * history
 */

public class CarPartStateFragment extends BaseFragment implements IStateContract.View {

    private static final String[] STATES = new String[]{"良好", "过期未保养", "需保养"};
    @BindView(R.id.rv_parts_state)
    RecyclerView mRvPartsState;
    Unbinder unbinder;
    @BindView(R.id.btn_test)
    Button mBtnTest;
    private PartStateAdapter mPartStateAdapter;
    private StatePresenter mPresenter;
    private ArrayList<CarPartState> mPartsState;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View mView = inflater.inflate(R.layout.framgent_car_state, null);
        unbinder = ButterKnife.bind(this, mView);
        return mView;
    }

    @Override
    public void onStart() {
        super.onStart();
        mPresenter = new StatePresenter(this, null);
        mPresenter.showPartsState();
    }

    @Override
    public void showPartsState(final ArrayList<CarPartState> list) {
        mPartStateAdapter = new PartStateAdapter(R.layout.item_part_state, list);
        mRvPartsState.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRvPartsState.addItemDecoration(new SpaceItemDecoration(5, 5));
        mRvPartsState.setAdapter(mPartStateAdapter);
        mPartStateAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(getActivity(), PartStateActivity.class);
                CarPartState state = mPartsState.get(position);
                intent.putExtra("PartState", state);
                startActivity(intent);
            }
        });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.btn_test)
    public void onViewClicked() {
//        Intent intent = new Intent(this,);
//        startActivity(intent);
    }

    private class PartStateAdapter extends BaseQuickAdapter<CarPartState, BaseViewHolder> {

        public PartStateAdapter(int layoutResId, @Nullable List data) {
            super(layoutResId, data);
        }


        @Override
        protected void convert(BaseViewHolder helper, CarPartState item) {
            helper.setText(R.id.tv_part, item.getName());
            helper.setText(R.id.tv_state, STATES[item.getStateType()]);
            switch (item.getStateType()) {
                case 0:
                    helper.setTextColor(R.id.tv_state, ContextCompat.getColor(getActivity(), R.color.car_part_state_normal));
                    break;
                case 1:
                    helper.setTextColor(R.id.tv_state, ContextCompat.getColor(getActivity(), R.color.car_part_state_fault));
                    break;
                case 2:
                    helper.setTextColor(R.id.tv_state, ContextCompat.getColor(getActivity(), R.color.car_part_state_damaged));
                    break;
                default:
                    helper.setTextColor(R.id.tv_state, ContextCompat.getColor(getActivity(), R.color.car_part_state_unknow));
                    break;
            }

        }
    }

}
